package com.example.demo.information.service.impl;

import com.example.demo.information.entity.Bjxx;
import com.example.demo.information.entity.School;
import com.example.demo.information.repository.BjxxRepository;
import com.example.demo.information.service.BjxxService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @author littlecar
 * @date 2020/3/23 15:19
 */
@Service
public class BjxxServiceImpl implements BjxxService {
    @Autowired
    private BjxxRepository bjxxRepository;

    @Override
    public List<Bjxx> findByXxxxIdAndNameLike(Long xxxxId,String name) {
        if(xxxxId!=null){
            return bjxxRepository.findByXxxxIdAndNameLike(xxxxId,name);
        }else{
            return bjxxRepository.findByNameLike(name);
        }
    }

    @Override
    public List<Bjxx> findPageByXxxxIdAndNameLike(Long xxxxId,String name, int pages, Integer limit) {
        if(xxxxId!=null){
            return bjxxRepository.findPageByXxxxIdAndNameLike(xxxxId,name, pages, limit);
        }else{
            return bjxxRepository.findPageByNameLike(name, pages, limit);
        }
    }

    @Override
    public List<Bjxx> findAll() {
        return bjxxRepository.findAll();
    }

    @Override
    public List<Bjxx> findByXxxxId(Long xxxxId) {
        if(xxxxId!=null){
            return bjxxRepository.findByXxxxId(xxxxId);
        }else{
            return bjxxRepository.findAll();
        }
    }

    @Override
    public List<Bjxx> findPageAll(int pages, Integer limit) {
        return bjxxRepository.findPageAll(pages, limit);
    }

    @Override
    public List<Bjxx> findPageByXxxxId(Long xxxxId, int pages, Integer limit) {
        if(xxxxId!=null){
            return bjxxRepository.findPageByXxxxId(xxxxId,pages, limit);
        }else{
            return bjxxRepository.findPageAll(pages, limit);
        }
    }

    @Override
    public List<Bjxx> findByName(Long xxxxId,String name) {
        Bjxx bjxx = bjxxRepository.findByXxxxIdAndName(xxxxId, name);
        List<Bjxx> bjxxList = new ArrayList<>();
        if(bjxx!=null){
            bjxxList.add(bjxx);
        }
        return bjxxList;
    }

    @Override
    public Bjxx findById(Long id) {
        return bjxxRepository.findById(id).get();
    }

    @Override
    public List<Bjxx> findByBjbh(Long xxId,String bjbh) {
        return bjxxRepository.findByXxxxIdAndBjbh(xxId,bjbh);
    }

    @Override
    public void save(Bjxx bjxx) {
        bjxxRepository.save(bjxx);
    }

    @Override
    public void deleteById(Long bjId) {
        bjxxRepository.deleteById(bjId);
    }

    @Override
    public List<Bjxx> findBySchoolIdAndBzrId(Long school_id, Long old_bzrId) {
        return bjxxRepository.findBySchoolIdAndBzrId(school_id, old_bzrId);
    }

    @Override
    public Set<Bjxx> findBySchoolId(Long xxId) {
        return bjxxRepository.findBySchoolId(xxId);
    }

    /**
     * 根据传来的List<String> (如果没有，就创建对应班级)创建对应的班级
     * @param xxbjmcList List<xxId@@bjmc>
     * @return
     */
    @Override
    public void createBjxxByXxbjmcList(List<String> xxbjmcList, Map<Long, School> xxMap) {
        for(String xxbjmc :xxbjmcList){
            if(StringUtils.isBlank(xxbjmc)||xxbjmc.lastIndexOf("@@")==-1){continue;}
            String[] split = xxbjmc.split("@@");
            Long xxId;
            try {
                 xxId = Long.parseLong(split[0]);
            } catch (NumberFormatException e) {
                continue;
            }
            if(!xxMap.containsKey(xxId)){continue;}
            School school = xxMap.get(xxId);
            String bjmc = split[1];
            Bjxx bjxx = bjxxRepository.findByXxxxIdAndName(xxId,bjmc);
            if(bjxx==null){
                bjxx = new Bjxx();
                bjxx.setSchool(school);
                bjxx.setName(bjmc);
                bjxxRepository.save(bjxx);
            }

        }
    }

}
